package samsung.sds.systemMonitor.userOperationMonitor.controller;

import java.net.UnknownHostException;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import samsung.sds.system.common.baseQuery.model.PageData;
import samsung.sds.system.common.model.QueryParam;
import samsung.sds.system.utils.CommonTools;
import samsung.sds.systemMonitor.userOperationMonitor.model.MonitorOperationEntity;
import samsung.sds.systemMonitor.userOperationMonitor.model.MonitorUserBaseInfo;
import samsung.sds.systemMonitor.userOperationMonitor.service.UserOperationMonitorService;
import samsung.sds.systemMonitor.userOperationMonitor.utils.Constants;

/**
 * 
 * 获取用户操作日志
 * 
 * @author lixin.qiu
 * @createDate 2016-01-18
 * 
 */
@Controller
@RequestMapping("/systemMonitor/userOperationMonitor")
public class UserOperationMonitorController {

	@Autowired
	private UserOperationMonitorService userOperationMonitorServiceImpl;

	/**
	 * 很名单用户的跳转页面
	 * 
	 * @param model
	 * @param request
	 * @return
	 * @throws UnknownHostException
	 */
	@RequestMapping("/blackUserRedirect")
	public String blackUserRedirect(ModelMap model, HttpServletRequest request)
			throws UnknownHostException {
		MonitorOperationEntity monitorOperationEntity = MonitorUserBaseInfo
				.getMonitorOperationInfo(request);
		// 黑名单用户用户名入成Anonymous
		monitorOperationEntity.setCreateUser("Anonymous");
		monitorOperationEntity.setOpType(Constants.BLACKUSER_FLAG);
		ServletContext servletContext = request.getSession()
				.getServletContext();
		String blackUserDesc = (String) servletContext
				.getAttribute("system_ip_blackUser_desc");
		monitorOperationEntity.setModuleName(blackUserDesc);
		userOperationMonitorServiceImpl.insertEntity(monitorOperationEntity);
		request.setAttribute("blackUserDesc",blackUserDesc);
		return "/systemMonitor/userOperationMonitor/blackUserRedirect";
	}

	/**
	 * 接收请求，返回用户操作List的jsp页面
	 * 
	 * @param request
	 * @param modelMap
	 * @return
	 */
	@RequestMapping(value = "/userOperationMonitorList", method = RequestMethod.POST)
	public String loadList(HttpServletRequest request, ModelMap modelMap) {
		String pageType = request.getParameter("pageType");
		modelMap.addAttribute("pageType", pageType);
		modelMap.addAttribute("currentTime", CommonTools.getCurrentDateTime());
		return "/systemMonitor/userOperationMonitor/userOperationMonitorList";
	}

	/**
	 * 接收请求，封装参数，返回PagaData对象
	 * 
	 * @param request
	 * @param modelMap
	 * @return
	 */
	@RequestMapping(value = "/getUserOperationMonitorList", method = RequestMethod.POST)
	@ResponseBody
	public PageData<MonitorOperationEntity> getUserOperationMonitorList(
			HttpServletRequest request) {

		QueryParam param = CommonTools.getQueryParam(request);
		return userOperationMonitorServiceImpl.queryListPage(param);
	}

	/**
	 * 查看用户操作的详情
	 * 
	 * @param operationId
	 * @param modelMap
	 * @return
	 */
	@RequestMapping(value = "/userOperationMonitorDetails", method = RequestMethod.POST)
	public String userOperationMonitorDetails(String operationId,
			ModelMap modelMap) {

		MonitorOperationEntity monitorOperationEntity = userOperationMonitorServiceImpl
				.findEntityById(operationId);
		// 存入MonitorOperationEntity对象
		modelMap.addAttribute("monitorOperationEntity", monitorOperationEntity);
		return "/systemMonitor/userOperationMonitor/userOperationMonitorDetails";
	}

	/**
	 * 捕获浏览器关闭事件,清除session
	 * 
	 * @param createUser
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/closeExploreAndMoveSession", method = RequestMethod.GET)
	public void closeExploreAndMoveSession(HttpServletRequest request) {
		request.getSession().invalidate();
	}
}
